﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;

namespace CommonLib
{
    public class FieldPrePrc
    {
        private AxMapControl m_axMapControl;
        //private XMLConfig m_readConfigDataPreHandle = new XMLConfig("SpatialData\\DataPreHandleConfig.xml");
        //private XMLConfig m_readConfig = new XMLConfig("MainConfig.xml");
        private List<bool> m_successFlag = new List<bool>();

        //private string m_jiancequName;
        //private string m_caiyangdianName;
        //private IFeatureLayer m_jiancequFeatureLayer;
        //private IFeatureLayer m_caiyangdianFeatureLayer;
        private List<int> m_selectFeatureOidList = new List<int>();
        private XMLConfig m_fieldHandleConfig;
        public FieldPrePrc()
        { 
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public FieldPrePrc(AxMapControl axMapControl, XMLConfig fieldHandleConfig)
        {
            m_axMapControl = axMapControl;
            m_fieldHandleConfig = fieldHandleConfig;
        }

        /// <summary>
        /// 通过图层名，设置图层字段
        /// </summary>
        /// <param name="layerName">图层名</param>
        /// <returns></returns>
        public List<bool> setFieldsByLayerName(string layerName)
        {
            List<bool> isOkFlag = new List<bool>(); ;
            List<fieldClass> fieldList = new List<fieldClass>();
            List<Fields> fields = new List<Fields>();
            List<string> nameList = m_fieldHandleConfig.GetAttributeValueListFromNodeName("layer", "name", layerName, "field", "name");
            List<string> aliasnameList = m_fieldHandleConfig.GetAttributeValueListFromNodeName("layer", "name", layerName, "field", "aliasname");
            List<string> typeList = m_fieldHandleConfig.GetAttributeValueListFromNodeName("layer", "name", layerName, "field", "type");
            List<string> typeLengthList = m_fieldHandleConfig.GetAttributeValueListFromNodeName("layer", "name", layerName, "field", "typeLength");

            for (int i = 0; i < nameList.Count; i++)
            {
                fieldList.Add(new fieldClass()
                {
                    name = nameList[i],
                    aliasname = aliasnameList[i],
                    type = typeList[i],
                    typeLength = typeLengthList[i]
                });
            }
            try
            {
                IFeatureLayer pFeatureLayer = AETools.GetFeatureLayer(m_axMapControl, layerName, 0);
                setFields(pFeatureLayer.FeatureClass, fieldList);
                isOkFlag = setFields(pFeatureLayer.FeatureClass, fieldList);
                //isOkFlag = true;
                return isOkFlag;
            }
            catch
            {
                for (int i = 0; i < fieldList.Count; i++)
                {
                    isOkFlag.Add(false);
                }
                return isOkFlag;
            }
        }

        /// <summary>
        /// 通过shape文件，设置字段
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <param name="fileName">文件名</param>
        /// <param name="layerName"></param>
        /// <returns>图层名：采样点、耕地质量监测区、分等要素</returns>
        public List<bool> setFieldsByShpFile(string filePath, string fileName, string layerName)
        {
            List<bool> isOkFlag = new List<bool> ();;
            List<fieldClass> fieldList = new List<fieldClass>();
            List<string> name = ConfigsAllocator.DataPreHandleConfig.GetAttributeValueListFromNodeName("layer", "name", layerName, "field", "name");
            List<string> aliasname = ConfigsAllocator.DataPreHandleConfig.GetAttributeValueListFromNodeName("layer", "name", layerName, "field", "aliasname");
            List<string> type = ConfigsAllocator.DataPreHandleConfig.GetAttributeValueListFromNodeName("layer", "name", layerName, "field", "type");
            List<string> typeLength = ConfigsAllocator.DataPreHandleConfig.GetAttributeValueListFromNodeName("layer", "name", layerName, "field", "typeLength");
            for (int i = 0; i < name.Count; i++)
            {
                fieldList.Add(new fieldClass()
                {
                    name = name[i],
                    aliasname = aliasname[i],
                    type = type[i],
                    typeLength = typeLength[i]
                });
            }
            try
            {

                IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
                IFeatureWorkspace pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(filePath, 0) as IFeatureWorkspace;
                IFeatureClass pFeatureClass = pFeatureWorkspace.OpenFeatureClass(fileName + ".shp");
                isOkFlag = setFields(pFeatureClass, fieldList);
                //isOkFlag = true;
                return isOkFlag;
            }
            catch
            {
                for(int i=0;i<fieldList.Count;i++)
                {
                    isOkFlag.Add(false);
                }  
                return isOkFlag;
            }
        }

        private List<bool> setFields(IFeatureClass featureClass, List<fieldClass> fieldList)//设置字段
        {
            #region 在编辑之前检查编辑条件可行性
            IDataset pDataset = (IDataset)featureClass;
            if (null == pDataset) return null;
            #endregion
            try
            {
                IField pField = new FieldClass();
                IFieldEdit pFieldEdit = pField as IFieldEdit;
                bool isInclude = false;//包含标识

                for (int i = 0; i < fieldList.Count; i++)
                {
                    isInclude = false;
                    for (int j = 0; j < featureClass.Fields.FieldCount; j++)
                    {
                        if (fieldList[i].name.Equals(featureClass.Fields.Field[j].Name))
                        {
                            isInclude = true;
                            break;
                        }
                    }
                    if (!isInclude)
                    {
                        pFieldEdit.Name_2 = fieldList[i].name;
                        switch (fieldList[i].type.Trim())
                        {
                            case "esriFieldTypeString":
                                pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
                                pFieldEdit.Length_2 = int.Parse(fieldList[i].typeLength);//文本型的限定长度
                                break;
                            case "esriFieldTypeDouble":
                                pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;
                                pFieldEdit.Precision_2 = int.Parse(fieldList[i].typeLength);//double型的限定精度
                                break;
                            case "esriFieldTypeInteger":
                                pFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;
                                break;
                            case "esriFieldTypeDate":
                                pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDate;
                                break;
                        }
                        pFieldEdit.AliasName_2 = fieldList[i].aliasname;
                        featureClass.AddField(pFieldEdit);
                        m_successFlag.Add(true);                       
                    }
                    else m_successFlag.Add(false);
                      
                }
                return m_successFlag;
            }
            catch
            {
                m_successFlag .Add (false );              
                return m_successFlag;
            }
            finally
            { }

        }
        //字段类

        public class fieldClass
        {
            public string name { get; set; }
            public string aliasname { get; set; }
            public string type { get; set; }
            public string typeLength { get; set; }

        }
    }
}
